package com.jing.cloud.service.configinit;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jing.cloud.zookeeper.ZookeeperHelp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;

public class InitConfigService {
    private final static Logger logger = LoggerFactory.getLogger(InitConfigService.class);

    private String path = System.getProperty("user.dir");//jar包所在目录名
    //导入配置文件，固定文件名
    public void importConfigByFile(){

        try {
            FileInputStream fileInputStream=new FileInputStream(new File(path+File.separator+"liandoudou.conf"));
            FileChannel fileChannel= fileInputStream.getChannel();
            ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
            StringBuilder stringBuilder=new StringBuilder();
            while(true){
                int r= fileChannel.read(byteBuffer);
                if(r==-1){
                    break;
                }
                byte[] array = new byte[r];
                byteBuffer.flip();
                byteBuffer.get(array);
                stringBuilder.append(new String(array));
                byteBuffer.clear();

            }
            Map<String,String> config=new HashMap<String,String>();
            config.put("liandoudouconfig",stringBuilder.toString());
            ZookeeperHelp zookeeperHelp=new ZookeeperHelp();
            zookeeperHelp.setConfig(config);
        } catch (Exception e) {
            logger.error("importConfigByFile" ,e);
        }
    }
    public void importDbConfig(){
        try {
            FileInputStream fileInputStream=new FileInputStream(new File(path+File.separator+"dbconfig.conf"));
            FileChannel fileChannel= fileInputStream.getChannel();
            ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
            StringBuilder stringBuilder=new StringBuilder();
            while(true){
                int r= fileChannel.read(byteBuffer);
                if(r==-1){
                    break;
                }
                byte[] array = new byte[r];
                byteBuffer.flip();
                byteBuffer.get(array);
                stringBuilder.append(new String(array));
                byteBuffer.clear();

            }
            JSONObject jsonObject=JSONObject.parseObject(stringBuilder.toString());

            JSONArray jsonArray=jsonObject.getJSONArray("serviceConfigs");
            for(int i=1;i<jsonArray.size();i++){
                JSONObject temp=JSONObject.parseObject(jsonArray.getString(i));
                String serviceName=temp.getString("serviceName");
                String version=temp.getString("version");
                Map<String,String> config=new HashMap<String,String>();
                config.put(serviceName+"-"+version,jsonArray.getString(i));
                ZookeeperHelp zookeeperHelp=new ZookeeperHelp();
                zookeeperHelp.setConfig(config);
            }
        } catch (IOException e) {
            logger.error("importDbConfig error" ,e);
        }
    }

    public void importThirfdPartyConfig(){
        try {
            FileInputStream fileInputStream=new FileInputStream(new File(path+File.separator+"thirdPartyConfig.conf"));
            FileChannel fileChannel= fileInputStream.getChannel();
            ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
            StringBuilder stringBuilder=new StringBuilder();
            while(true){
                int r= fileChannel.read(byteBuffer);
                if(r==-1){
                    break;
                }
                byte[] array = new byte[r];
                byteBuffer.flip();
                byteBuffer.get(array);
                stringBuilder.append(new String(array));
                byteBuffer.clear();
            }
            JSONObject jsonObject=JSONObject.parseObject(stringBuilder.toString());
            JSONArray jsonArray=jsonObject.getJSONArray("thirdPartyConfigs");
            for(int i=1;i<jsonArray.size();i++){
                JSONObject temp=JSONObject.parseObject(jsonArray.getString(i));
                for(String s:temp.keySet()){
                    Map<String,String> config=new HashMap<String,String>();
                    config.put(s,temp.getString(s));
                    ZookeeperHelp zookeeperHelp=new ZookeeperHelp();
                    zookeeperHelp.setConfig(config);
                }
            }
        } catch (IOException e) {
            logger.error("importThirfdPartyConfig error" ,e);
        }

    }
    public void deleteAllConfig(){
        ZookeeperHelp zookeeperHelp=new ZookeeperHelp();
        zookeeperHelp.delete("/linkdood");

    }

    public String getData(String configName){
        ZookeeperHelp zookeeperHelp=new ZookeeperHelp();

        return zookeeperHelp.getData(configName);
    }
}
